package example2;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 处理字节的节点流
 */
public class FileInputOutputStreamTest {

    @Test
    public void read1() {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("1.jpg");
            int next = fileInputStream.read();
            while (next != -1) {
                System.out.println(next);
                next = fileInputStream.read();
            }
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }


    @Test
    public void read2() {
        FileInputStream fileInputStream = null;
        byte[] bytes = new byte[3];

        try {
            fileInputStream = new FileInputStream("1.jpg");
            int num = fileInputStream.read(bytes);
            while (num != -1) {
                System.out.println(num);
                for (int i = 0; i < num; i++) {
                    System.out.println(bytes[i]);
                }
                num = fileInputStream.read(bytes);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    @Test
    public void write() {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("2.txt");
            String s = "1818";
            byte[] bytes = s.getBytes();
            fileOutputStream.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    @Test
    public void readwrite() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        byte[] bytes = new byte[1000];

        try {
            fileInputStream = new FileInputStream("1.txt");
            fileOutputStream = new FileOutputStream("2.txt");
            int num = fileInputStream.read(bytes);
            while (num != -1) {
                fileOutputStream.write(bytes, 0, num);
                num = fileInputStream.read(bytes);
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        } 
        finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }


}


